package org.clever.arkdemo.livestream.common.stream.utils;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class LiveStreamCodec {

    private static final char[] DIGITS_LOWER =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

//    /*
//     * KEY+ streamName + txTime
//     */
//    private static String getSafeUrl(String key, String streamName, long txTime) {
//        String input = new StringBuilder().
//                append(key).
//                append(streamName).
//                append(Long.toHexString(txTime).toUpperCase()).toString();
//
//        String txSecret = null;
//        try {
//            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
//            txSecret  = byteArrayToHexString(
//                    messageDigest.digest(input.getBytes("UTF-8")));
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//
//        return txSecret == null ? "" :
//                new StringBuilder().
//                        append("txSecret=").
//                        append(txSecret).
//                        append("&").
//                        append("txTime=").
//                        append(Long.toHexString(txTime).toUpperCase()).
//                        toString();
//    }

    /**
     * MD5加密
     * @param args
     * @return
     */
    public static String md5(Object... args) {
        if (args == null || args.length == 0) {
            return null;
        }

        StringBuilder stringBuilder = new StringBuilder();
        for (Object arg : args) {
            stringBuilder.append(arg);
        }
        String input = stringBuilder.toString();
        String txSecret = null;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            txSecret  = byteArrayToHexString(
                    messageDigest.digest(input.getBytes(Charset.forName("UTF-8"))));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return txSecret == null ? "" : txSecret;
    }

    /**
     * byte数组转16进制字符串
     * @param data
     * @return
     */
    public static String byteArrayToHexString(byte[] data) {
        char[] out = new char[data.length << 1];

        for (int i = 0, j = 0; i < data.length; i++) {
            out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS_LOWER[0x0F & data[i]];
        }
        return new String(out);
    }

    /**
     * sha256 加密
     */
    public static String sha256(Object... args) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Object arg : args) {
            stringBuilder.append(arg);
        }
        String input = stringBuilder.toString();
        String txSecret = null;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            txSecret  = byteArrayToHexString(
                    messageDigest.digest(input.getBytes(Charset.forName("UTF-8"))));
        } catch (NoSuchAlgorithmException e) {
           e.printStackTrace();
        }
        return txSecret == null ? "" : txSecret;
    }
}
